package com.thinkcube.edutainment.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.Util;
import com.thinkcube.edutainment.domain.data.CourseData;

/**
 * Shanuka @Cyberlmj 9:26:38 PM Email: shanuka.gayashan@cyberlmj.com
 */
public class ImageAdapterCircleGallery extends BaseAdapter {

	private final Context mContext;

	List<CourseData> placeList;

	private final LayoutInflater inflater;

	private final DisplayImageOptions options;
	private final ImageLoader imageLoader;

	public ImageAdapterCircleGallery(Context c,
			final List<CourseData> placeListList) {
		mContext = c;
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.placeList = placeListList;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.icon)
				.showImageForEmptyUri(R.drawable.icon)
				.showImageOnFail(R.drawable.icon).cacheOnDisc(true)
				.displayer(new SimpleBitmapDisplayer()).build();

		imageLoader = ImageLoader.getInstance();

	}

	@Override
	public int getCount() {
		return placeList.size();
	}

	@Override
	public Object getItem(int position) {
		return getPosition(position);
	}

	@Override
	public long getItemId(int position) {
		return getPosition(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// ImageView i = new ImageView(mContext);
		// position = getPosition(position);
		// i.setImageResource(mImageIds[position]);
		// i.setLayoutParams(new Gallery.LayoutParams(400, 400));
		// i.setScaleType(ImageView.ScaleType.FIT_XY);

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.cover_item, null);
			convertView.setLayoutParams(new Gallery.LayoutParams(330, 390));
			holder.courseName = (TextView) convertView
					.findViewById(R.id.course_name);
			holder.teacherPlace = (TextView) convertView
					.findViewById(R.id.teacher_place);
			holder.courseImage = (ImageView) convertView
					.findViewById(R.id.course_image);
			holder.teacherImageView = (ImageView) convertView
					.findViewById(R.id.teacher_place_holder_image_view);
			holder.teacherName = (TextView) convertView
					.findViewById(R.id.teacher_name_text_view);
			holder.price = (TextView) convertView
					.findViewById(R.id.price_textview);
			holder.studentCount = (TextView) convertView
					.findViewById(R.id.student_count);
			holder.ratingBar = (RatingBar) convertView
					.findViewById(R.id.ratingBar1);
			position = getPosition(position);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.courseName.setText(placeList.get(position).getCourseTitle());
		holder.studentCount.setText(placeList.get(position).getNumOfStudents());
		holder.price.setText(placeList.get(position).getCourseDisplayPrice());
		holder.teacherName
				.setText(placeList.get(position).getTeacherFullName());
		System.out.println("reting value "
				+ placeList.get(position).getCourseRating());
		if (placeList.get(position).getCourseRating() != null) {
			System.out.println("rati");
			holder.ratingBar.setRating(Float.valueOf(placeList.get(position)
					.getCourseRating()) / 2);

		}

		holder.teacherPlace.setText(placeList.get(position)
				.getTeacherDescription());
		imageLoader.displayImage(placeList.get(position)
				.getCourseCoverImageSnall(), holder.courseImage, options);
		imageLoader.displayImage(placeList.get(position).getTeacherIcon(),
				holder.teacherImageView, options);
		return convertView;
	}

	public int checkPosition(int position) {
		return getPosition(position);
	}

	int getPosition(int position) {
		// if (position >= placeList.size()) {
		// position = position % placeList.size();
		// }
		return position;
	}

	class ViewHolder {
		TextView courseName;
		ImageView courseImage;
		ImageView teacherImageView;
		TextView teacherPlace;
		TextView teacherName;
		TextView price;
		TextView studentCount;
		RatingBar ratingBar;
	}
}